package com.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.bean.CraftShow;
import com.bean.CraftStandardType;
import com.opensymphony.xwork2.ActionSupport;
import com.serviceImpl.CraftStandardTypeServiceImpl;
import com.serviceImpl.UtilSupport;

public class CraftStandardTypeAction extends ActionSupport {

	private UtilSupport util;	//公用类
	private CraftStandardTypeServiceImpl craftStandardTypeBiz; //标准service
	
	//集合
	private List<CraftStandardType> allCraftStandardType;	//接收所有的标准
	private JSONArray cpStandardJsons;
	
	//查询字段
	private String craftStdCode;	//标准编码
	private String craftStdName;	//标准名称
	private int offset;			//当前页
	private int pageSize=10;		//每页显示几条
	private int totalcount;		// 总记录数
	private int totalpage; 		// 总页数
	
	//实体类
	private CraftStandardType craftStandardType;	//标准类

	
	//set、get
	
	public CraftStandardTypeAction() {
		
		allCraftStandardType=new ArrayList();	//初始化集合
		cpStandardJsons = new JSONArray();
		
	}
	
	public UtilSupport getUtil() {
		return util;
	}

	public void setUtil(UtilSupport util) {
		this.util = util;
	}

	public CraftStandardTypeServiceImpl getCraftStandardTypeBiz() {
		return craftStandardTypeBiz;
	}

	public void setCraftStandardTypeBiz(
			CraftStandardTypeServiceImpl craftStandardTypeBiz) {
		this.craftStandardTypeBiz = craftStandardTypeBiz;
	}

	public String getCraftStdCode() {
		return craftStdCode;
	}

	public void setCraftStdCode(String craftStdCode) {
		this.craftStdCode = craftStdCode;
	}

	public String getCraftStdName() {
		return craftStdName;
	}

	public void setCraftStdName(String craftStdName) {
		this.craftStdName = craftStdName;
	}

	public CraftStandardType getCraftStandardType() {
		return craftStandardType;
	}

	public void setCraftStandardType(CraftStandardType craftStandardType) {
		this.craftStandardType = craftStandardType;
	}
	
	public List<CraftStandardType> getAllCraftStandardType() {
		return allCraftStandardType;
	}

	public void setAllCraftStandardType(List<CraftStandardType> allCraftStandardType) {
		this.allCraftStandardType = allCraftStandardType;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalcount() {
		return totalcount;
	}

	public void setTotalcount(int totalcount) {
		this.totalcount = totalcount;
	}

	public int getTotalpage() {
		return totalpage;
	}

	public void setTotalpage(int totalpage) {
		this.totalpage = totalpage;
	}
	
	public JSONArray getCpStandardJsons() {
		return cpStandardJsons;
	}

	public void setCpStandardJsons(JSONArray cpStandardJsons) {
		this.cpStandardJsons = cpStandardJsons;
	}

	/**
	 * 获取所有工艺标准集合
	 * @return
	 */
	private List<CraftStandardType> getCstList(){
		HttpServletRequest request = ServletActionContext.getRequest();
 		StringBuffer sqlbf = new StringBuffer("select count(*) from audit.craft_standard_type WHERE 0=0 ");
 		
 		String	stdCode = "";
 		String nameSql = "";
 		String priceSql = "";
 		
 		try {
			this.craftStdCode = request.getParameter("code");
			if(craftStdCode!=null&&!craftStdCode.isEmpty()){
				craftStdCode=new String(craftStdCode.trim().getBytes("ISO-8859-1"),"UTF-8");
				
				stdCode = "and craft_std_code like '%"+craftStdCode+"%'";
				
				sqlbf.append(stdCode);
				
			}

			this.craftStdName = request.getParameter("name");
			if(craftStdName!=null&&!craftStdName.isEmpty()){
				craftStdName=new String(craftStdName.trim().getBytes("ISO-8859-1"),"UTF-8");
				
				nameSql="and craft_std_name = '"+craftStdName+"'";
				
				sqlbf.append(nameSql);
			}
			
			

			String sql =sqlbf.toString();
			
			totalcount = util.getTotalCount(sql);

			totalpage = totalcount % pageSize == 0 ? totalcount / pageSize
					: totalcount / pageSize + 1;

			offset = getPageOffset();
			//表名
			String table="audit.craft_standard_type";
			//主键
			String pk="craft_std_type_id";
			//排序字段
			String sort="craft_std_type_id";
			//当前页数
			int PageNumber=offset+1;
			
			int PageSize=pageSize;
			
			//查询条件
			String options=(stdCode+nameSql)==null?"":"0=0"+stdCode+nameSql;
			allCraftStandardType=craftStandardTypeBiz.getAllCraftStandardType(table, pk, sort, PageNumber, PageSize, options);
		
 		} catch (Exception e) {
			// TODO: handle exception
		}
 		return allCraftStandardType;
	}
	/**
	 * 查询所有的标准
	 */
	public String craftStandardTypeShow(){
		getCstList();
		return "show";
	}
	
	/**
	 * 获取所有工艺部件标准详情
	 * @return
	 */
	public String getAllStandards(){
		List<CraftStandardType> allCstList = getCstList();
		cpStandardJsons = JSONArray.fromObject(allCstList); 
		return "allStandards";
	}
	
	// Added by JSL : 获取翻页偏移量(实际上是将要翻到的页面的页索引，页索引从 0 开始)
	private int getPageOffset() {
		HttpServletRequest request=ServletActionContext.getRequest();
		String ofst = request.getParameter("offset");
		int idx = 0;
		if(ofst!=null){
			idx = Integer.valueOf(ofst);
			idx = idx < 0 ? 0 : idx;                        // 超过第一页时，不再翻页
			idx = idx >= totalpage ? (totalpage-1) : idx;	// 超过最后一页时，不再翻页		
		}
		return idx;
	}
	
}
